package com.aoxon.cargo.component.n;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.aoxon.cargo.R;
import com.aoxon.cargo.activity.PurSupplierDetailsActivity;
import com.aoxon.cargo.bean.Supplier;
import com.baidu.mapapi.map.ItemizedOverlay;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.PopupOverlay;
import com.baidu.platform.comapi.basestruct.GeoPoint;
import com.google.gson.Gson;

public class CargoItemizedOverlay extends ItemizedOverlay<CargoOverlayItem> {

	private View popView;
	private PopupOverlay mPopupOverlay;
	private LayoutInflater inflater;
	private boolean initialize = false;
	private TextView store_name;
	private TextView address;
	private TextView telephone;
	private MapView mapView;
	private View sup_detail;

	public CargoItemizedOverlay(MapView mapView, PopupOverlay popupOverlay) {
		super(mapView.getContext().getResources()
				.getDrawable(R.drawable.icon_gcoding), mapView);
		this.inflater = LayoutInflater.from(mapView.getContext());
		this.mPopupOverlay = popupOverlay;
		this.mapView = mapView;
		// TODO Auto-generated constructor stub
	}

	@Override
	protected boolean onTap(int index) {
		// TODO Auto-generated method stub
		final CargoOverlayItem item = (CargoOverlayItem) getItem(index);
		Supplier supplier = item.getSupplier();
		if (!initialize) {
			popView = inflater.inflate(R.layout.pub_map_pop, null);
			store_name = (TextView) popView.findViewById(R.id.store_name);
			telephone = (TextView) popView.findViewById(R.id.telephone);
			address = (TextView) popView.findViewById(R.id.address);
			sup_detail = popView.findViewById(R.id.sup_detail);
		}

		sup_detail.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(mapView.getContext(),
						PurSupplierDetailsActivity.class);
				Bundle data = new Bundle();
				data.putString("strSupplier",
						new Gson().toJson(item.getSupplier()));
				intent.putExtras(data);
				Activity a = (Activity) mapView.getContext();
				a.startActivity(intent);
			}
		});
		store_name.setText(supplier.getStoreName());
		address.setText(supplier.getSupCity() + supplier.getsupMarket());
		telephone.setText(supplier.getSupPhone());
		mPopupOverlay.showPopup(popView, item.getPoint(), 20);
		return true;
	}

	@Override
	public boolean onTap(GeoPoint pt, MapView mMapView) {
		if (mPopupOverlay != null) {
			mPopupOverlay.hidePop();
		}
		return false;
	}

	public void setItems(List<Supplier> suppliers) {
		for (int i = 0; i < suppliers.size(); i++) {
			addItem(new CargoOverlayItem(suppliers.get(i)));
		}
	}

	public void setItem(Supplier supplier) {
		addItem(new CargoOverlayItem(supplier));
	}
}
